<?php

/**
 * Trida modelu pro maklere.
 *
 * @author David Dusek
 */
class AgentModel extends NTableSelection {
    public function __construct(NConnection $connection) {
        parent::__construct("agent", $connection);
    }
    
    /**
     * Zformatuje telefoni cislo do podoby +420 XXX XXX XXX. Zformatovani 
     * probehne pokud se zadane telefoni cislo sklada z 9 nebo 12 cisel a
     * dalsich libovolnych znaku (napr + nebo mezera), ktere budou odstraneny.
     * @param String $phone vstupni retezec ktery ma byt zformatovan.
     * @return String 
     */
    public static function formatPhone($phone) {
        $phoneFormated = FALSE;
        $phoneWSF = preg_replace("/[^0-9]/", "", $phone);
        if (strlen($phoneWSF) == 9) {
            $phoneFormated = preg_replace("/([0-9]{3})([0-9]{3})([0-9]{3})/", "+420 $1 $2 $3", $phoneWSF);            
        } if (strlen($phoneWSF) == 12) {
            $phoneFormated = preg_replace("/420([0-9]{3})([0-9]{3})([0-9]{3})/", "+420 $1 $2 $3", $phoneWSF);
        }
        
        if ($phoneFormated !== FALSE) {
            $phone = $phoneFormated;
        }
        
        return $phone;
    }
}
